// pages/order/order.js
const app = getApp();
Page({
  data: {
    items: [],
    isShowConfirm: false,
    log: '',
    orderid: '',
  },
  onShow: function (options) {
    var that = this;
    that.getScrollWidth();
    that.listupdate();
  },
  onUnload: function () {
    wx.reLaunch({
      url: '../index/index', //指定url
    })
  },

  listupdate: function () {
    var that = this
    let transportWorkerId = app.globalData.user.id;
    // let transportWorkerId = '1404762246076194817';
    let urlBase = 'http://47.111.122.217:9001/transport/transportOrder/select/'
    let urlR = urlBase + transportWorkerId + '/1/100'
    // console.log(urlR);
    wx.request({
      url: urlR,
      method: 'get',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        // console.log(res.data.data.data);
        var items = res.data.data.data;
        var order = [];
        for (let i = 0; i < items.length; i++) {
          // console.log(items[i].state);
          if (items[i].state == 5) {
            items[i].tag = '待出发';
            items[i].feetag = '特殊上报';
          }
          else if (items[i].state == 6) {
            items[i].tag = '待装货';
            items[i].feetag = '特殊上报';
          }
          else if (items[i].state == 7) {
            items[i].tag = '待卸货';
            items[i].feetag = '特殊上报';
          }
          else if (items[i].state == 8) {
            items[i].tag = '待回场';
            items[i].feetag = '特殊上报';
          }
          else if (items[i].state == 9) {
            items[i].feetag = '随车清单';
          }else {
            items[i].feetag = '已完成'
          }
          order.push(items[i]);
        }
        that.setData({
          items: order
        })
      }
    })
  },
  getScrollWidth: function () {
    var scrollWidth = wx.getSystemInfoSync().windowWidth;
    this.setData({
      scrollWidth: scrollWidth / 2 * 5,
      windowWidth: scrollWidth
    })
  },
  scrollleft: function (e) {
    console.log(e);
  },
  lowerRight: function (e) {
    console.log(e);
  },
  //日期转换
  fromDate: function (date, fmt) {
    var that = this;
    date = new Date(date)
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
    }

    let obj = {
      "M+": date.getMonth() + 1,
      "d+": date.getDay(),
      "h+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds()
    }

    for (let key in obj) {
      if (new RegExp(`(${key})`).test(fmt)) {
        let str = obj[key] + ''
        fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : that.padLeftZero(str))
      }
    }
    return fmt
  },
  padLeftZero: function (str) {
    return ('00' + str).substr(str.length)
  },
  //阶段按键
  next: function (event) {
    var that = this;
    let orderid = event.currentTarget.dataset.menuid;
    let state = event.currentTarget.dataset.state;
    let date = new Date();
    let fmtDate = that.fromDate(date, 'yyyy-MM-dd hh:mm:ss');
    // console.log(fmtDate);
    if (state == 5) {
      wx.showModal({
        title: '确认',
        content: '确认出发',
        success(res) {
          if (res.confirm) {
            wx.request({
              url: 'http://47.111.122.217:9001/transport/transportProcess/update/depart',
              method: 'post',
              data: {
                'id': orderid,
                'trueDepartTime': fmtDate
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                if (res.data.success) {
                  that.listupdate();
                  wx.showToast({
                    title: '完成出发',
                    icon: 'success',
                    duration: 2000
                  })
                } else {
                  return false;
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    if (state == 8) {
      wx.showModal({
        title: '确认',
        content: '确认回场',
        success(res) {
          if (res.confirm) {
            wx.request({
              url: 'http://47.111.122.217:9001/transport/transportProcess/update/end',
              method: 'put',
              data: {
                'id': orderid,
                'trueDepartTime': fmtDate
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                if (res.data.success) {
                  that.listupdate();
                  wx.showToast({
                    title: '完成回场',
                    icon: 'success',
                    duration: 2000
                  })
                } else {
                  return false;
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    if (state == 6) {
      wx.navigateTo({
        url: '../loadgoods/loadgoods?orderid=' + orderid
      })
    }
    if (state == 7) {
      wx.navigateTo({
        url: '../unloadgoods/unloadgoods?orderid=' + orderid
      })
    }
  },
  updateSpecial: function (event) {
    var that = this;
    let orderid = event.currentTarget.dataset.menuid;
    let state = event.currentTarget.dataset.state;
    if (state == 9) {
      wx.navigateTo({
        url: '../fee/fee?orderid=' + orderid
      })
    }
    else if (state >= 5 && state < 9) {
      that.setData({
        isShowConfirm: true,
        orderid: orderid
      })
    }else{
      wx.showToast({
        title: '已完成所有操作',
        icon: 'success',
        duration: 2000
      })
    }
  },
  //下面是弹窗
  setValue: function (e) {
    this.setData({
      log: e.detail.value
    })
  },
  cancel: function () {
    var that = this
    that.setData({
      isShowConfirm: false,
    })
  },
  confirmAcceptance: function () {
    var that = this
    let log = that.data.log;
    let id = that.data.orderid;
    let urlR = 'http://47.111.122.217:9001/transport/transportProcess/update/log/' + id + '/' + log;
    // console.log(urlR); 
    wx.request({
      url: urlR,
      method: 'put',
      data: {},
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.success) {
          wx.showToast({
            title: '完成上报',
            icon: 'success',
            duration: 2000
          })
        } else {
          return false;
        }
      }
    }),
      that.setData({
        isShowConfirm: false,
      })
  },
})